home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr31 / hades.zip / MERGE.C < prev    next >
C/C++ Source or Header  |  1992-04-14  |  4KB  |  187 lines

  1.  
  2. /***************
  3. **
  4. **  merge.c
  5. **  last revised: april 12, 1992
  6. **
  7. **  Merge merges two *optimized* dictionaries to one sorted dictionary.
  8. **
  9. **  Usage: merge <filename> [-f filename] [-o outfile]
  10. **
  11. **  Written for DESPERATE password-cracker with HADES encryption engine by
  12. **  Remote.
  13. **
  14. **  Copyright (C)1992, Zabkar
  15. **
  16. **  root@waves.hacktic.nl (Zabkar)
  17. **  root@room101.hacktic.nl (Remote)
  18. **
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23.  
  24.  
  25. FILE *outfile;
  26. FILE *infile1, *infile2;
  27.  
  28.  
  29. /***************
  30.  haltusage()
  31.  prints correct usage and exits
  32. ****************/
  33.  
  34. void haltusage()
  35. {
  36.   fprintf(stderr,
  37.   "Merge version 1.00 alpha, Copyright (C)1992 Zabkar\n"\
  38.   "DESPERATE password-cracker 1.0 alpha using HADES engine by Remote.\n\n"\
  39.   "Usage: merge <wordfile> [-f wordfile] [-o outfile]\n\n"\
  40.   "\t-f: read from 'wordfile' instead of stdin\n"\
  41.   "\t-o: write to 'outfile' instead of stdout\n\n"\
  42.   "WARNING: all wordfiles must be optimized, or you'll get unpredictable "\
  43.   "results\n\n"\
  44.   "no -f specified: wordfile read from stdin\n"\
  45.   "no -o specified: output written to stdout\n");
  46.   exit(0);
  47. }
  48.  
  49.  
  50. /***************
  51.  mergelist()
  52.  merges sorted files inf1 with inf2 and writes result to file of.
  53. ***************/
  54.  
  55. void mergelists(FILE *inf1, FILE *inf2, FILE *of)
  56. {
  57.     char word1[9], word2[9];
  58.     int r1, r2, s;
  59.  
  60.     r1 = fscanf(inf1, "%s", word1);
  61.     r2 = fscanf(inf2, "%s", word2);
  62.  
  63.     while ((r1 > 0) && (r2>0))
  64.     {
  65.         s = strcmp(word1, word2);
  66.         if (s <= 0)
  67.         {
  68.             fprintf(of, "%s\n", word1);
  69.             r1 = fscanf(inf1, "%s", word1);
  70.         }
  71.         if (s > 0)
  72.         {
  73.             fprintf(of, "%s\n", word2);
  74.             r2 = fscanf(inf2, "%s", word2);
  75.         }
  76.         if (!s)
  77.             r2 = fscanf(inf2, "%s", word2);
  78.     }
  79.  
  80.     if (r1 > 0 || r2 > 0)         /* any more words in either file? */
  81.       if (r1 <= 0)
  82.           {
  83.           fprintf(of, "%s\n", word2);
  84.           while (fscanf(inf2, "%s", word2) >= 0)
  85.               fprintf(of, "%s\n", word2);
  86.           }
  87.       else
  88.           {
  89.           fprintf(of, "%s\n", word1);
  90.           while (fscanf(inf1, "%s", word1) >= 0)
  91.               fprintf(of, "%s\n", word1);
  92.           }
  93. }
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101. /***************
  102.  main()
  103.  main function of merge
  104. ****************/
  105.  
  106. main(char argc, char **argv)
  107. {
  108.   char f1name[80], f2name[80], oname[80];
  109.   int i;
  110.  
  111.   strcpy(f2name, "");
  112.   strcpy(oname, "");
  113.  
  114.   if (argc < 2)
  115.      haltusage();
  116.   else
  117.   {
  118.     strcpy(f1name, argv[1]);
  119.     for (i=2; i<argc; i++)
  120.     {
  121.       switch(argv[i][0])
  122.       {
  123.       case '-': switch(toupper(argv[i][1]))
  124.           {
  125.             case 'F' : if (strlen(argv[i]) > 2)
  126.                  strcpy(f2name, &argv[i][2]);
  127.                    else if (argc < (i+2))
  128.                  haltusage();
  129.                    else
  130.                  strcpy(f2name, argv[++i]);
  131.                    break;
  132.             case 'O' : if (strlen(argv[i]) > 2)
  133.                  strcpy(oname, &argv[i][2]);
  134.                    else if (argc < (i+2))
  135.                  haltusage();
  136.                    else
  137.                  strcpy(oname, argv[++i]);
  138.                    break;
  139.             default  : haltusage();
  140.           }
  141.           break;
  142.       default : haltusage();
  143.       }
  144.     }
  145.   }
  146.  
  147.   infile1 = fopen(f1name, "rt");
  148.   if (!infile1)
  149.     {
  150.     fprintf(stderr, "merge: %s: Couldn't open file\n", f1name);
  151.     exit(0);
  152.     }
  153.  
  154.   if (strcmp(f2name,""))
  155.     infile2 = fopen(f2name, "rt");
  156.   else
  157.     infile2 = stdin;
  158.  
  159.   if (!infile2)
  160.     {
  161.     fprintf(stderr, "merge: %s: couldn't open file\n", f2name);
  162.     exit(0);
  163.     }
  164.  
  165.   if (strcmp(oname, ""))
  166.     outfile = fopen(oname, "wt");
  167.   else
  168.     outfile = stdout;
  169.  
  170.   if (!outfile)
  171.     {
  172.     fprintf(stderr, "%s: could't create file\n", oname);
  173.     exit(0);
  174.     }
  175.  
  176.   mergelists(infile1, infile2, outfile);
  177.  
  178.   fclose(infile1);
  179.   fclose(infile2);
  180.   fclose(outfile);
  181.  
  182.   }
  183.  
  184. /* EOF MERGE.C */
  185.  
  186.  
  187.